package org.whuims.web.admin;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springside.modules.web.Servlets;
import org.whuims.entity.Standard;
import org.whuims.service.account.ShiroDbRealm;
import org.whuims.service.standard.StandardService;

import javax.servlet.ServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by Qikai on 2016/12/21.
 */

@Controller
@RequestMapping(value = "/admin/std")
public class StandardAdminController {

    private static final String PAGE_SIZE = "10";
    @Autowired
    private StandardService stdService;

    @RequestMapping(value = "list", method = RequestMethod.GET)
    public String list(@RequestParam(value = "page", defaultValue = "1") int pageNumber,
                       @RequestParam(value = "page.size", defaultValue = PAGE_SIZE) int pageSize, Model model,
                       ServletRequest request) {
        Map<String, Object> searchParams = Servlets.getParametersStartingWith(request, "search_");
        Page<Standard> stds = stdService.getStandardList(searchParams, pageNumber, pageSize, null);
        model.addAttribute("stds", stds);
        model.addAttribute("sortType", "auto");
        // 将搜索条件编码成字符串，用于排序，分页的URL
        model.addAttribute("searchParams", Servlets.encodeParameterStringWithPrefix(searchParams, "search_"));
        return "admin/std/list";
    }

    @RequestMapping(value = "delete/{id}", method = RequestMethod.GET)
    public String delete(@PathVariable("id") Long id, RedirectAttributes redirectAttributes) {
        stdService.delete(id);
        redirectAttributes.addFlashAttribute("message", "删除条目成功");
        return "redirect:/admin/std/list";
    }

    @RequestMapping(value = "detail/{id}", method = RequestMethod.GET)
    public String detail(@PathVariable("id") Long id, Model model) {
        Standard std = stdService.getStandard(id);
//        List<Standard> refStds=stdService.getRefStandards(std);
        model.addAttribute("std", std);
//        model.addAttribute("refStds", refStds);
        return "admin/stddetail";
    }

    @RequestMapping(value = "ref/{id}", method = RequestMethod.GET)
    public String refStds(@PathVariable("id") Long id, Model model) {
        Standard std = stdService.getStandard(id);
        List<Standard> refStds = stdService.getRefStandards(std);
        model.addAttribute("std", std);
        model.addAttribute("refStds", refStds);
        return "admin/stdref";
    }

    @RequestMapping(value = "elements/{id}", method = RequestMethod.GET)
    public String elements(@PathVariable("id") Long id, Model model) {
        Standard std = stdService.getStandard(id);
        model.addAttribute("std", std);
        return "admin/elements";
    }

    @RequestMapping(value = "terms/{id}", method = RequestMethod.GET)
    public String terms(@PathVariable("id") Long id, Model model) {
        Standard std = stdService.getStandard(id);
        model.addAttribute("std", std);
        return "admin/stdterms";
    }


    @RequestMapping(value = "edit/{id}", method = RequestMethod.GET)
    public String edit(@PathVariable("id") Long id, Model model) {
        Standard std = stdService.getStandard(id);
        model.addAttribute("std", std);
        return "std/stdedit";
    }

    /**
     * 取出Shiro中的当前用户Id.
     */
    private Long getCurrentUserId() {
        ShiroDbRealm.ShiroUser user = (ShiroDbRealm.ShiroUser) SecurityUtils.getSubject().getPrincipal();
        return user.id;
    }
}
